function init(){

    console.info("function::init()")
    var req = opensocial.newDataRequest();
    var fields = ["State"];
    req.add(req.newFetchPersonAppDataRequest(opensocial.DataRequest.PersonId.VIEWER, fields), 'viewer_data');
    req.add(req.newFetchPersonRequest(opensocial.DataRequest.PersonId.OWNER), 'owner');
    req.send(handleInit);
}

function handleInit(response){
    console.info("function::handelInit()")
    
    var owner = response.get("owner");
    owner = owner.getData();

    if (owner.isViewer()) {
        if (response.get("viewer_data").hadError()) {
            console.error("Fatal error on retrieving viewer_data");
        }
        else {
            var state = response.get("viewer_data").getData();
            if (state[owner.getId()] == undefined) {
                console.info("Going to initialization screen");
		        fetchPage("app-init", handlePageAppInit);
            }
        }
    }
    else {
        console.info("TODO");
    }
};

function handlePageAppInit(obj){
    //obj.text contains the text of the page that was requested  
    var html = obj.text;
    document.getElementById('app-container').innerHTML = html;
};

function appInitSubmitCode(){
	formEl = document.getElementById('appCodeForm');
	var appCode = formEl.elements['appCode'].value;
    console.info("submitCode called: " + appCode);    
}

// lib
function fetchPage(pagename, pagecode){
    var params = {};
	var baseURL = "http://walh-testing-01.googlecode.com/svn/trunk/walh-testing-01/walh-testing-01/src/";
	var baseExtension = ".html";
	var baseParameters = "nocache=" + new Date().getTime();
	
    params[gadgets.io.RequestParameters.CONTENT_TYPE] = gadgets.io.ContentType.TEXT;
    var url = baseURL + pagename + baseExtension + "?" + baseParameters;
    
	gadgets.io.makeRequest(url, pagecode, params);
};

